package com.gitee.neuray.security.admin.executor.handler;

import com.gitee.neuray.security.common.msg.ObjectRestResponse;
import com.gitee.neuray.security.common.util.CommonUtil;
import com.gitee.neuray.security.mail.feign.WBMailServiceFeignClient;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@Slf4j
public class WBMailXxJob {

    @Autowired
    WBMailServiceFeignClient client;

    /**
     * 网报一级审批每月15日提醒
     */
    @XxlJob("firstApproveStartMailHandler")
    public ReturnT<String> firstApproveStartMailHandler(String param) throws Exception {
        SimpleDateFormat simpleDateFormat =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报一级审批每月15日提醒邮件执行开始......"+simpleDateFormat.format(new Date()));
        XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报一级审批每月15日提醒邮件执行开始......"+simpleDateFormat.format(new Date()));
        ObjectRestResponse res = null;
        try {
            res = client.firstApproverStart();
        } catch (Exception e) {
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报一级审批每月15日提醒邮件执行失败");
            XxlJobLogger.log(CommonUtil.getExceptionDtail(e));
            log.error(CommonUtil.getExceptionDtail(e));
            throw e;
        }
        if(res!=null && res.isRel()){
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报一级审批每月15日提醒邮件执行成功......");
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报一级审批每月15日提醒邮件执行成功......");
            return ReturnT.SUCCESS;
        }else {
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报一级审批每月15日提醒邮件执行失败......");
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报一级审批每月15日提醒邮件执行失败......");
            return ReturnT.FAIL;
        }
    }

    /**
     * 网报二级审批每月19日提醒
     */
    @XxlJob("secondApproveStartMailHandler")
    public ReturnT<String> secondApproveStartMailHandler(String param) throws Exception {
        SimpleDateFormat simpleDateFormat =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报二级审批每月19日提醒邮件执行开始......"+simpleDateFormat.format(new Date()));
        XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报二级审批每月19日提醒邮件执行开始......"+simpleDateFormat.format(new Date()));
        ObjectRestResponse res = null;
        try {
            res = client.secondApproverStart();
        } catch (Exception e) {
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报二级审批每月19日提醒邮件执行失败");
            XxlJobLogger.log(CommonUtil.getExceptionDtail(e));
            log.error(CommonUtil.getExceptionDtail(e));
            throw e;
        }
        if(res!=null && res.isRel()){
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报二级审批每月19日提醒邮件执行成功......");
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报二级审批每月19日提醒邮件执行成功......");
            return ReturnT.SUCCESS;
        }else {
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报二级审批每月19日提醒邮件执行失败......");
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报二级审批每月19日提醒邮件执行失败......");
            return ReturnT.FAIL;
        }
    }

    /**
     * 网报一级审批每月18日提醒
     */
    @XxlJob("firstApproveEndMailHandler")
    public ReturnT<String> firstApproveEndMailHandler(String param) throws Exception {
        SimpleDateFormat simpleDateFormat =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报一级审批每月18日提醒邮件执行开始......"+simpleDateFormat.format(new Date()));
        XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报一级审批每月18日提醒邮件执行开始......"+simpleDateFormat.format(new Date()));
        ObjectRestResponse res = null;
        try {
            res = client.firstApproverEnd();
        } catch (Exception e) {
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报一级审批每月18日提醒邮件执行失败");
            XxlJobLogger.log(CommonUtil.getExceptionDtail(e));
            log.error(CommonUtil.getExceptionDtail(e));
            throw e;
        }
        if(res!=null && res.isRel()){
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报一级审批每月18日提醒邮件执行成功......");
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报一级审批每月18日提醒邮件执行成功......");
            return ReturnT.SUCCESS;
        }else {
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报一级审批每月18日提醒邮件执行失败......");
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报一级审批每月18日提醒邮件执行失败......");
            return ReturnT.FAIL;
        }
    }

    /**
     * 网报二级审批每月24日提醒
     */
    @XxlJob("secondApproveEndMailHandler")
    public ReturnT<String> secondApproveEndMailHandler(String param) throws Exception {
        SimpleDateFormat simpleDateFormat =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报二级审批每月24日提醒邮件执行开始......"+simpleDateFormat.format(new Date()));
        XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报二级审批每月24日提醒邮件执行开始......"+simpleDateFormat.format(new Date()));
        ObjectRestResponse res = null;
        try {
            res = client.secondApproverEnd();
        } catch (Exception e) {
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报二级审批每月24日提醒邮件执行失败");
            XxlJobLogger.log(CommonUtil.getExceptionDtail(e));
            log.error(CommonUtil.getExceptionDtail(e));
            throw e;
        }
        if(res!=null && res.isRel()){
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报二级审批每月24日提醒邮件执行成功......");
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报二级审批每月24日提醒邮件执行成功......");
            return ReturnT.SUCCESS;
        }else {
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报二级审批每月24日提醒邮件执行失败......");
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报二级审批每月24日提醒邮件执行失败......");
            return ReturnT.FAIL;
        }
    }

    /**
     * 网报三级审批每月23日提醒
     */
    @XxlJob("thirdApproveMailHandler")
    public ReturnT<String> thirdApproveMailHandler(String param) throws Exception {
        SimpleDateFormat simpleDateFormat =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报三级审批每月23日提醒邮件执行开始......"+simpleDateFormat.format(new Date()));
        XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报三级审批每月23日提醒邮件执行开始......"+simpleDateFormat.format(new Date()));
        ObjectRestResponse res = null;
        try {
            res = client.thirdApprover();
        } catch (Exception e) {
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报三级审批每月23日提醒邮件执行失败");
            XxlJobLogger.log(CommonUtil.getExceptionDtail(e));
            log.error(CommonUtil.getExceptionDtail(e));
            throw e;
        }
        if(res!=null && res.isRel()){
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报三级审批每月23日提醒邮件执行成功......");
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报三级审批每月23日提醒邮件执行成功......");
            return ReturnT.SUCCESS;
        }else {
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报三级审批每月23日提醒邮件执行失败......");
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报三级审批每月23日提醒邮件执行失败......");
            return ReturnT.FAIL;
        }
    }

    /**
     * 网报四级审批每月25日提醒
     */
    @XxlJob("fourthApproveMailHandler")
    public ReturnT<String> fourthApproveMailHandler(String param) throws Exception {
        SimpleDateFormat simpleDateFormat =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报四级审批每月25日提醒邮件执行开始......"+simpleDateFormat.format(new Date()));
        XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报四级审批每月25日提醒邮件执行开始......"+simpleDateFormat.format(new Date()));
        ObjectRestResponse res = null;
        try {
            res = client.fourthApprover();
        } catch (Exception e) {
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报四级审批每月25日提醒邮件执行失败");
            XxlJobLogger.log(CommonUtil.getExceptionDtail(e));
            log.error(CommonUtil.getExceptionDtail(e));
            throw e;
        }
        if(res!=null && res.isRel()){
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报四级审批每月25日提醒邮件执行成功......");
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报三级审批每月23日提醒邮件执行成功......");
            return ReturnT.SUCCESS;
        }else {
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>网报四级审批每月25日提醒邮件执行失败......");
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>网报四级审批每月25日提醒邮件执行失败......");
            return ReturnT.FAIL;
        }
    }
}
